package com.shixi.shixitraining.shixitraining.ui.tabmine.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shixi.shixitraining.shixitraining.R;
import com.shixi.shixitraining.shixitraining.base.TabhostBaseFragment;
import com.shixi.shixitraining.shixitraining.http.CallbackImple;
import com.shixi.shixitraining.shixitraining.http.HttpUtil;
import com.shixi.shixitraining.shixitraining.test.TestCollectTrainingBean;
import com.shixi.shixitraining.shixitraining.ui.tabmine.adapter.CollectTrainingAdapter;
import com.shixi.shixitraining.shixitraining.ui.tabmine.entity.JobCollectBean;
import com.shixi.shixitraining.shixitraining.util.ToastUtil;
import com.shixi.shixitraining.shixitraining.util.UserManager;
import com.shixi.shixitraining.shixitraining.view.recycleview.RecycleItemClickListener;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

import static com.shixi.shixitraining.shixitraining.constant.Constants.MYCOLLECT;

/**
 * Created by shenyan on 2018/7/26.
 */

public class CollectTrainingFragment extends TabhostBaseFragment {

    private List<TestCollectTrainingBean> data;
    private TextView mNodata;

    @Override
    protected int getlayoutId() {
        return R.layout.fragment_collect_training;
    }

    @Override
    protected void initPager() {


        data = new ArrayList<>();
        data.add(new TestCollectTrainingBean("联合国新星全球领导人和全球培养计划", "美国/纽约", "5天", "2018.07.10"));
        data.add(new TestCollectTrainingBean("联合国新星全球领导人和全球培养计划", "美国/纽约", "5天", "2018.07.10"));
        data.add(new TestCollectTrainingBean("联合国新星全球领导人和全球培养计划", "美国/纽约", "5天", "2018.07.10"));
        data.add(new TestCollectTrainingBean("联合国新星全球领导人和全球培养计划", "美国/纽约", "5天", "2018.07.10"));
        data.add(new TestCollectTrainingBean("联合国新星全球领导人和全球培养计划", "美国/纽约", "5天", "2018.07.10"));
        data.add(new TestCollectTrainingBean("联合国新星全球领导人和全球培养计划", "美国/纽约", "5天", "2018.07.10"));
        data.add(new TestCollectTrainingBean("联合国新星全球领导人和全球培养计划", "美国/纽约", "5天", "2018.07.10"));
        data.add(new TestCollectTrainingBean("联合国新星全球领导人和全球培养计划", "美国/纽约", "5天", "2018.07.10"));

        mNodata = (TextView) mContentView.findViewById(R.id.tv_nodata);

        String token = UserManager.getInstance().getToken();
        String userId = UserManager.getInstance().getUserId();
        requestTrainingCollect(token,userId);

    }

    private void requestTrainingCollect(String token, String userId) {
     String url=MYCOLLECT+"&token="+token+"&userId="+userId+"&type="+2;

        HttpUtil.getInstance().getApiInterface().trainingCollect(url).enqueue(new CallbackImple<JobCollectBean>() {
            @Override
            public void onError(Call<JobCollectBean> call, Throwable t) {
                ToastUtil.showShortToast(getContext(),t.getMessage());
            }

            @Override
            public void onSuccess(Call<JobCollectBean> call, JobCollectBean response) {

                if (response.getStatus()==200){

                    if (response.getData()!=null){

                        List<JobCollectBean.DataBean.ListsBean> lists = response.getData().getLists();
                        for (int i=0;i<lists.size();i++){
                            JobCollectBean.DataBean.ListsBean listsBean = lists.get(i);
                        }

                        RecyclerView mRcView = (RecyclerView) mContentView.findViewById(R.id.rcview_collect_training);
                        LinearLayoutManager manager=new LinearLayoutManager(getContext());
                        manager.setOrientation(LinearLayoutManager.VERTICAL);
                        mRcView.setLayoutManager(manager);

                        CollectTrainingAdapter adapter = new CollectTrainingAdapter(getContext(), data, Glide.with(getContext()));
                        mRcView.setAdapter(adapter);
                        mRcView.addOnItemTouchListener(new RecycleItemClickListener(getContext()) {
                            @Override
                            public void itemClickListener(View view, int position) {
//                                startActivity(new Intent(getContext(), TrainingDetailActivity.class));
                            }
                        });

                    }else {
                        ToastUtil.showShortToast(getContext(),"数据为空");
                    }

                }else {
                    mNodata.setVisibility(View.VISIBLE);
                    ToastUtil.showShortToast(getContext(),"数据加载失败");
                }

            }
        });


    }



}
